<template>
  <div>
    <!-- <van-nav-bar left-text="返回" left-arrow @click-left="onClickLeft" /> -->
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-nav-bar
        :title="title"
        left-text=""
        left-arrow
        @click-left="onClickLeft"
      >
      </van-nav-bar>
      <!-- 帖子发表的主要信息 -->
      <div class="selfInfo">
        <van-icon
          name="delete-o"
          style="
            position: absolute !important;
            right: 5% !important;
            top: 3% !important;
            font-size: 19px !important;
          "
          @click="deletePost"
        />
        <div class="infoTitle">
          <span>{{ postDetail.title }}</span>
        </div>
        <div class="infoTopPic">
          <img :src="postDetail.avatar" alt="" />
        </div>
        <div class="infoName">
          <span style="padding-top: 4px">{{ postDetail.userName }}</span>
          <p class="infoTime" style="margin: 0; padding-top: 4px">
            {{ postDetail.updateTime }}
          </p>
        </div>
        <!-- 发表的内容名 -->
        <div class="postListContent">
          {{ postDetail.intro }}
        </div>
        <div class="listPic">
          <img :src="postDetail.coverImgUrl" alt="" />
        </div>
        <div class="thumb">
          <li><van-icon name="good-job-o" :badge="postDetail.readNum" /></li>

          <!-- <li><van-icon name="eye-o" badge="5" /></li> -->
          <li><van-icon name="chat-o" :badge="postDetail.zan" /></li>
        </div>
      </div>

      <!-- 底部评论 -->
      <div style="background: #f4f4f4; overflow: hidden" class="allComment">
        <!-- 帖子评论 -->
        <!-- <van-pull-refresh v-model="isLoading" @refresh="onRefresh"> -->
        <div
          class="commentBox"
          v-for="(item, index) in postComment"
          :key="index"
        >
          <!-- 删除 -->

          <van-icon
            name="delete-o"
            style="
              position: absolute !important;
              right: 5% !important;
              top: 5% !important;
              font-size: 18px !important;
            "
            @click="deleteIcon(index)"
          />
          <div class="commentTopPic">
            <img :src="item.avatar" alt="" />
          </div>
          <div class="commentName">
            <p>{{ item.userName }}</p>
          </div>
          <div class="commentContent">{{ item.commentContent }}</div>
          <div class="commentThumb">
            <li @click="likeMe(index)">
              <van-icon name="good-job-o" ref="changeColor" />
            </li>

            <van-popup
              v-model="show"
              :overlay="bool"
              closeable
              position="bottom"
              :style="{ height: '120px' }"
            >
              <van-cell-group style="position: relative">
                <van-field
                  v-model="message"
                  rows="2"
                  autosize
                  type="textarea"
                  maxlength="100"
                  placeholder="请输入评论内容"
                  show-word-limit
                />
                <button
                  style="
                    font-size: 12px;
                    position: absolute;
                    right: 0;
                    bottom: -30px;
                  "
                  @click="replay"
                >
                  回复
                </button>
              </van-cell-group>
            </van-popup>
            <li>
              <van-icon name="chat-o" is-link @click="showPopup" /><span>{{
                item.readNum
              }}</span>
            </li>
          </div>

          <!-- <div class="replyContent">
            <p class="replayName">@{{ item.userName }}:</p>
            <p class="replayInfo">{{ messages }}</p>
          </div> -->
        </div>
        <!-- </van-pull-refresh> -->

        <!-- 刷新 -->

        <!-- 第二个 -->
        <!-- <div class="commentBox">
          <van-icon
            name="delete-o"
            style="
              position: absolute !important;
              right: 5% !important;
              top: 5% !important;
              font-size: 18px !important;
            "
          />
          <div class="commentTopPic">
            <img src="../assets/zhifeiji.jpg" alt="纸飞机" />
          </div>
          <div class="commentName">
            <p>姓名1</p>
          </div>
          <div class="commentContent">这是我发表的第一篇内容</div>
          <div class="commentThumb">
            <li><van-icon name="good-job-o" /><span>1</span></li>
            <li><van-icon name="chat-o" /><span>117</span></li>
          </div>
          <div class="replyContent">
            <p class="replayName">姓名:</p>
            <p class="replayInfo">回复的内容</p>
          </div>
        </div> -->
        <div class="commentAll">
          <input
            type="text"
            placeholder="发表评论:"
            class="publishComment"
            v-model="publishComments"
          />
          <van-button type="default" @click="getComment">评论</van-button>
        </div>
      </div>
    </van-pull-refresh>
  </div>
</template>

<script>
import { Toast } from "vant";
import {
  postDetail,
  deletePost,
  postComment,
  publishSpeech,
  deleteSpeech,
  requestSpeech,
} from "../api/api";
// import tieziComment from "../components/TieziComment.vue";

export default {
  name: "PostDetail",
  components: {},
  data() {
    return {
      // postList: "",
      postsId: "",
      postDetail: "",
      title: "",
      isLoading: false,
      postComment: {},
      publishComments: "",
      commentId: "",
      show: false,
      message: "",
      bool: false,
      messages: "",
      styles: "change",
      color: "red",
      zan: 0,
      number: 0,
    };
  },
  methods: {
    onRefresh() {
      setTimeout(() => {
        Toast("刷新成功");
        this.isLoading = false;
        // this.count++;
      }, 1000);
      console.log(Number(this.$route.query.postsId));
      postComment(Number(this.$route.query.postsId)).then((res) => {
        console.log(res);
        if (res.code == 0) {
          this.postComment = res.rows;
          this.postComment.reverse();
          // for (let i = 0; i < this.postComment.length; i++) {
          //   this.commentId = res.rows[i].commentId;
          // }
        }
        // console.log(res.rows[0].commentId);
      });
    },
    onClickLeft() {
      Toast("返回");
      window.history.back(-1);
    },
    deletePost() {
      deletePost(this.$route.query.postsId).then((res) => {
        console.log(res);
        Toast("帖子删除成功");
      });
    },

    // 下面评论
    // 点赞改变
    likeMe(a) {
      if (this.number % 2 == 0) {
        for (let i = 0; i < this.postComment.length; i++) {
          if (a == i) {
            this.$refs.changeColor[a].style = "color:red";
            //  this.zan++
            this.number++;
          }
        }
      } else {
        this.$refs.changeColor[a].style = "color:balck";
        this.number++;
      }
    },

    showPopup() {
      this.show = true;
    },
    // 发表评论
    getComment() {
      publishSpeech(this.$route.query.postsId, this.publishComments).then(
        (res) => {
          if (res.code == 0) {
            console.log(res);
            Toast("评论成功");
            this.publishComments = "";
          } else {
            Toast("请去登录");
          }
        }
      );
    },
    replay() {
      requestSpeech(
        this.$route.query.postsId,
        this.commentId,
        this.message
      ).then((res) => {
        if (res.code == 0) {
          console.log(res);
          this.messages = this.message;
          this.message = "";
          Toast("评论成功");
        } else {
          Toast("评论失败");
        }
      });
    },
    // 删除评论
    deleteIcon(index) {
      for (let i = 0; i < this.postComment.length; i++) {
        if (index == i) {
          deleteSpeech(this.commentId).then((res) => {
            console.log(this.commentId);
            console.log(res);
            Toast("删除成功");
          });
        }
      }
    },
  },
  created() {
    // postList().then((res) => {
    //   console.log(res);
    //   if (res.code == 0) {
    //     this.postList = res.rows;
    //   }
    // });
    postDetail(this.$route.query.postsId).then((res) => {
      console.log(res);
      if (res.code == 0) {
        this.postDetail = res.data;
        this.postsId = res.data.postsId;
      }
    });
    postComment(this.$route.query.postsId).then((res) => {
      console.log(res);
      if (res.code == 0) {
        this.postComment = res.rows;
        this.postComment.reverse();
        for (let i = 0; i < this.postComment.length; i++) {
          this.commentId = res.rows[i].commentId;
        }
      }
      // console.log(res.rows[0].commentId);
    });
  },
};
</script>

<style scoped  lang="less">
/deep/.van-nav-bar__content {
  background-color: #cc0000;
}
/deep/.van-nav-bar .van-icon {
  color: #fff;
}
/deep/.van-nav-bar__text {
  color: #fff;
}
/deep/.van-nav-bar__title {
  color: #fff;
}
/deep/.van-button--info {
  background-color: #cc0000;
  border: 1px solid #cc0000;
}
.selfInfo {
  position: relative;
  margin: auto;
  width: 100%;
  /* height: 350px; */
  background-color: #fdf8f8;
  /* border: 1px solid red; */
}
.infoTitle {
  margin: 20px 0 10px 20px;
  /* width: 100px; */
  height: 34px;
  line-height: 34px;
  text-align: left;
  /* border: 1px solid black; */
}

.infoTopPic {
  float: left;
  margin-top: 10px;
  margin-left: 20px;
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  /* border: 1px solid blue; */
  border-radius: 20px;
}
.infoTopPic > img {
  width: 40px;
  height: 40px;
  border-radius: 20px;
}
.infoName {
  margin-left: 60px;
  margin-top: 20px;
  padding-left: 10px;
  /* width: 140px; */
  /* height: 20px; */
  vertical-align: middle;
  font-size: 12px;
  text-align: left;
}
.postListContent {
  margin: 7% 0 2% 8%;
  /* width: 280px; */
  width: 80%;
  text-align: left;
  /* text-indent: 2em; */
}
.listPic {
  margin: auto;
  width: 86%;
}
.listPic > img {
  width: 100%;
}
.thumb {
  display: flex;
  flex-direction: row-reverse;
  list-style: none;
}
.thumb > li:first-child {
  padding-top: 10px;
  padding-right: 17%;
  /* text-align: right; */
}

.thumb > li:last-child {
  padding-right: 10%;
  padding-top: 10px;
  font-size: 16px;
  /* text-align: left; */
}
// 下面的评论
.change {
  color: red;
  font-size: 20px;
}
.commentBox {
  position: relative;
  margin: auto;
  margin-top: 5%;
  /* width: 340px; */
  width: 94%;
  /* border: 1px solid black; */
  box-shadow: 2px 2px rgb(233, 230, 230);
  border-radius: 10px;
  background: rgba(235, 200, 134, 0.26);
  margin-bottom: 12%;
}
.allComment > .commentBox:last-child {
  margin-bottom: 12%;
}
.commentTopPic {
  float: left;
  margin-top: 20px;
  margin-left: 50px;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  /* border: 1px solid blue; */
  border-radius: 20px;
}
.commentTopPic > img {
  width: 30px;
  height: 30px;
  border-radius: 15px;
}
.commentName {
  padding-top: 0.5%;
  margin-left: 60px;
  padding-left: 40px;
  vertical-align: middle;
  font-size: 13px;
  text-align: left;
}
.commentName > p {
  padding-top: 16px;
  vertical-align: middle;
}
.commentContent {
  margin: 14px 0 4px 50px;
  width: 250px;
  text-align: left;
  font-size: 14px;

  /* text-indent: 2em; */
}
.commentThumb {
  display: flex;
  flex-direction: row-reverse;
  /* justify-content: space-around; */
  list-style: none;
}
.commentThumb > li:first-child {
  padding-right: 40px;
  text-align: right;
  font-size: 14px;
}
.commentThumb > li:last-child {
  padding-right: 20px;
  font-size: 14px;
  text-align: left;
}
.replyContent {
  overflow: hidden;
  /* margin: 20px 0 4px 50px; */
  /* width: 250px; */
  margin: auto;
  width: 94%;
  /* height: 30px; */
  /* line-height: 10%; */
  text-align: left;
  font-size: 14px;
  box-shadow: 2px 2px #ccc;
  border-radius: 10px;
  background: rgba(235, 200, 134, 0.26);
}
.replyContent p {
  padding-left: 10px;
  float: left;
}
.commentAll {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 7%;
  background-color: white;
}
.publishComment {
  padding-left: 20px;
  width: 75%;
  height: 40px;
  margin: auto;
  /* margin-left: 6%; */
  position: fixed;
  left: 0;
  bottom: 0;
  font-size: 13px;
  border-radius: 5%;
  border: 1px solid #dcdfe6;
  outline: 0;
}
.commentAll > button {
  position: fixed;
  right: 0;
  bottom: 0;
  float: right;
  margin-right: 1%;
  width: 24%;
  height: 45px;
  border-radius: 8%;
}
.van-popup__close-icon--top-right {
  font-size: 16px;
}
</style>